package com.atguigu.gmall.auth.controller;

import com.atguigu.gmall.auth.service.AuthService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

@Controller
@RequestMapping
public class AuthController {


    @GetMapping("toLogin.html")
    public String toLogin(@RequestParam(value = "returnUrl",
                        required = false) String returnUrl, Model model,HttpServletRequest request){
//        returnUrl=http://search.gmall.com/search?keyword=%E5%8D%8E%E4%B8%BA&cid=225&props=6:%E9%AA%81%E9%BE%99
        if(StringUtils.isEmpty(returnUrl)){
            returnUrl = "http://www.gmall.com";
        }else{
            String queryString = request.getQueryString();
            returnUrl = queryString.substring(queryString.indexOf("returnUrl=")+"returnUrl=".length());
        }
        System.out.println(request.getQueryString());
        model.addAttribute("returnUrl" ,returnUrl);
        return "login";
    }
    @Autowired
    AuthService authService;
    //认证接口
    //returnUrl=&loginName=3123213&password=1231231&m1=2
    @PostMapping("login")
    public String login(String returnUrl, //登录成功的重定向地址
                        String loginName,
                        String password,
                        HttpServletRequest request,
                        HttpServletResponse response){
        //远程调用ums 使用账号密码查询userEntity对象
        //查询成功：将userEnity的数据转为 jwt的token存到cookie中
        //查询失败：跳转回登录页面提示
        String result = authService.login(loginName,password,request,response);
        if(StringUtils.isEmpty(result)){
            //登录失败：账号密码错误
            //跳转回登录页面 继续登录
            request.setAttribute("errorMsg" , "账号密码错误");
            return "login";
        }else if("0".equals(result)){
            //token构建时的异常
            //跳转到异常页面
            return "error";
        }
        //登录成功：重定向回之前的页面
        //重定向地址交给浏览器时 设置到响应报文头中，不支持中文 所以丢失
        // 中文可以使用UrlEncode编码，编码后浏览器会自动使用UrlDecode解码
        //http://search.gmall.com/search?keyword=华为&sort=1
//        String[] split = returnUrl.split("\\?");
//        String url = "";
//        if(split.length==2){
//            url = split[0]+"?";//http://search.gmall.com/search
//            //keyword=华为&sort=1
//            String[] split1 = split[1].split("&");
//            if(split1.length>0){
////                url+=split1[0]+"="; //keyword=华为
//                for (String s : split1) {
//                    String[] split2 = s.split("=");
//                    url+=split2[0]+"=";//keyword=
//                    String encode = URLEncoder.encode(split2[1]);
//                    url+=encode+"&";
//                }
//            }
//        }
//        System.out.println(url);
//        if(!StringUtils.isEmpty(url)){
//            url = url.substring(0,url.length()-1);
//        }

        return "redirect:"+returnUrl;
    }
}
